/**
 * 
 */
var oprater = {

	edit : function(id) {
		layer_show('编辑', contextPath+'/sys/role/goEdit/' + id, 650, 380);
	},
	add : function(id) {
		layer_show('添加', contextPath+'/sys/role/goAdd', 650, 380);
	},
	del : function(id){
		if(!id){
			return;
		}
		layer.confirm('确定要删除该角色？',function(){		
			$.ajax({
				url:contextPath+'/sys/role/del',
				data:$('#form-model').serialize(),
				type:'post',
				dataType:'json',
				data:{id:id},
				success:function(data){
					if(data.result){										
						layer.alert('删除成功！',{
						    time: 3000, //3s后自动关闭					  
						});
						oTable.draw();
					}else{
						layer.msg(data.msg,{
						    time: 3000, //3s后自动关闭					  
						});	
					}							
				},
				error:function(){
					layer.alert('删除失败，请联系管理员！');				
				}
			});	
		});	
	},
	menus:function(code){
		layer_show('菜单权限', contextPath+'/sys/role/rolemenu/' + code, 510, 520);
	}
}

var oTable;
$(function() {
	oTable = $('#table').tjTable({
		select:{
			type:'radio',
			name:'code'
		},
		ajax : {
			"url" : contextPath+"/sys/role/loadPage",
			"type" : "post",
			"data" : {
				'filters' : function() {
					var vo = [];
					var code = $('#code').val();
					var name = $('#name').val();
					if (code != null && code != ""){
						vo.push({
							name : 'code',
							oprate:'like',
							value : '%'+code+'%'
						});
					}										
					if (name != null && name != ""){
						vo.push({
							name : 'name',
							oprate:'like',
							value : '%'+name+'%'
						});
					}										
					return JSON.stringify(vo);
				}
			}
		},
		columns : [
			{
				"sTitle" : "角色编码",
				"data" : "code"
			},
			{
				"sTitle" : "角色名称",
				"data" : "name"
			},
			{
				"sTitle" : "角色描述",
				"data" : "memo"
			},																					
			{
				"sTitle" : "操作",
				"render" : function(data, type, row) {		
					
					var str = "<a href='javascript:oprater.del(\""
						+ row.id
						+ "\")' title='删除'><i class=\"Hui-iconfont\">&#xe6a1;</i>删除</a>&nbsp;&nbsp;";
					str = str
					+ "<a href='javascript:oprater.edit(\""
					+ row.id
					+ "\")' title='编辑'><i class=\"Hui-iconfont\">&#xe6df;</i>编辑</a>&nbsp;&nbsp;";										
					return str;
				}
			} ]
	});

	$('#search').click(function() {
		oTable.draw();
	});

	$('#add-btn').click(function() {
		oprater.add();
	});
	
	$('#menu-btn').click(function() {
		var roleCode = oTable.getSelects();
		if(roleCode){
			oprater.menus(roleCode);
		}		
	});
	
});